package com.thethinkfactory.whosappfor;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

import com.thethinkfactory.whosappfor.model.Event;
import com.thethinkfactory.whosappfor.model.WhatsAppForDataSource;
import com.thethinkfactory.whosappfor.model.db.TableEvent;

public class EventOverviewActivity extends ListActivity {

	private WhatsAppForDataSource datasource;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_overview);

		datasource = new WhatsAppForDataSource(this);
		datasource.open();

		datasource.deleteAllEvents();

		Event event = datasource.createEvent("TEST",
				"THIS IS A MOTHERFUCKING NEW EVENT");
		Log.i(EventOverviewActivity.class.getSimpleName(),
				"new event created: " + event.getId() + ", " + event.toString());
		
		Cursor c = datasource.getCursorAllEvents();

		ListAdapter listAdapter = new SimpleCursorAdapter(this,
				R.layout.listitem_event_overview, c, new String[] {
						TableEvent.COLUMN_TITLE, TableEvent.COLUMN_DESC },
				new int[] { R.id.eventTitle, R.id.eventDescription }, 0);

		setListAdapter(listAdapter);
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_event_overview, menu);
		return true;
	}
}
